<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="分类ID" prop="categoryId">
        <el-input
          v-model="queryParams.categoryId"
          placeholder="请输入分类ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="转换名称" prop="transName">
        <el-input
          v-model="queryParams.transName"
          placeholder="请输入转换名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
		  plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['datafactory:trans:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
		  plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['datafactory:trans:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
		  plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['datafactory:trans:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
		  plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['datafactory:trans:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="transList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="etl转换ID" align="center" prop="id"/>-->
      <el-table-column label="转换名称" align="center" prop="transName"/>
      <el-table-column label="任务分类" align="center" prop="categoryId"/>
      <el-table-column label="转换描述" align="center" prop="transDescription"/>
      <el-table-column label="转换资源库ID" align="center" prop="transRepositoryId"/>
      <el-table-column label="执行方式" align="center" prop="transType" />
      <el-table-column label="转换路径" align="center" prop="transPath" show-overflow-tooltip />

      <el-table-column label="添加者" align="center" prop="addUser" />
      <el-table-column label="转换参数" align="center" prop="transParams" show-overflow-tooltip />
      <el-table-column label="添加时间" align="center" prop="addTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.addTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="转换状态" align="center" prop="transStatus" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" >
        <template slot-scope="scope" v-if="scope.row.transStatus == 2">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['datafactory:trans:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['datafactory:trans:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-video-play"
            @click="handleStartTrans(scope.row)"
            v-hasPermi="['datafactory:trans:start']"
          >启动</el-button>
        </template>
      </el-table-column>
      <el-table-column label="运行中" align="center" class-name="small-padding fixed-width">
      <template slot-scope="scope" v-if="scope.row.transStatus == 1">
        <el-button
          size="mini"
          type="text"
          icon="el-icon-video-pause"
          @click="handleStopTrans(scope.row)"
          v-hasPermi="['datafactory:trans:Stop']"
        >停止</el-button>
      </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改转换对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="10">
            <el-form-item label="任务分类" prop="categoryId">
              <el-input v-model="form.categoryId" placeholder="请输入分类ID"/>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="转换名称" prop="transName">
              <el-input v-model="form.transName" placeholder="请输入转换名称"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="10">
            <el-form-item label="转换类型" prop="transType">
              <el-input v-model="form.transType" placeholder="请选择转换类型"/>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="转换路径" prop="transPath">
              <el-input v-model="form.transPath" placeholder="请输入转换路径"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="状态" prop="transStatus">
          <el-radio label="1">请选择字典生成</el-radio>
        </el-form-item>
        <el-form-item label="转换参数" prop="transParams">
          <el-input type="textarea" v-model="form.transParams" placeholder="请输入转换参数"/>
        </el-form-item>
        <el-form-item label="转换描述" prop="transDescription">
          <el-input type="textarea" v-model="form.transDescription"  placeholder="请输入转换描述"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listTrans, getTrans, delTrans, addTrans, updateTrans, startTrans, stopTrans} from "@/api/kettle/trans";

export default {
  name: "Trans",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 转换表格数据
      transList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 转换类型
      transTypeOptions: [],
      // 转换状态
      transStatusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        categoryId:null,
        transName:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        categoryId: [
          {required: true, message: "分类ID不能为空", trigger: "blur"}
        ],
        transName: [
          {required: true, message: "转换名称不能为空", trigger: "blur"}
        ],
        transType: [
          {required: true, message: "转换类型不能为空", trigger: "change"}
        ],
        transPath: [
          {required: true, message: "转换路径不能为空", trigger: "blur"}
        ],
        transParams: [
          {required: true, message: "转换参数不能为空", trigger: "blur"}
        ]

      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询转换列表 */
    getList() {
      this.loading = true;
      listTrans(this.queryParams).then(response => {
        this.transList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        categoryId:null,
        transName:null,
        transType:null,
        transStatus:0,
        transPath:null,
        transDescription:null,
        transParams:null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加转换";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id =  row.id  || this.ids
      getTrans(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改转换";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTrans(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTrans(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 启用按钮 */
    handleStartTrans(row){
      const id = row.id
      this.$confirm('是否启动转换编号为"' + id + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return startTrans(id)
      }).then(() => {
        this.getList();
        this.msgSuccess("启动成功");
      })
    },
    /** 停止按钮 */
    handleStopTrans(row){
      const id = row.id
      this.$confirm('是否停止转换编号为"' + id + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return stopTrans(id)
      }).then(() => {
        this.getList();
        this.msgSuccess("停止成功");
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids;
      this.$confirm('是否确认删除转换编号为"' + id + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTrans(id);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('datafactory/trans/export', {
        ...this.queryParams
      }, `datafactory_trans.xlsx`)
    }
  }
};
</script>
